package com.hzcx.app.simplechat.ui.chat;

import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.util.map.GdMapUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 地理位置界面
 */
public class ChatMapActivity extends BaseActivity {

    public static final String INTENT_LOCATION_LAT = "INTENT_LOCATION_LAT";
    public static final String INTENT_LOCATION_LNG = "INTENT_LOCATION_LNG";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.map_view)
    MapView mapView;

    private AMap aMap;
    private UiSettings uiSettings;
    private Marker marker;

    private double lat, lng;

    @Override
    public int getLayout() {
        return R.layout.activity_chat_map;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        tvTitle.setText("地理位置");
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapView.onCreate(savedInstanceState);
        //初始化地图控制器对象
        if (aMap == null) {
            aMap = mapView.getMap();
        }
        lat = getIntent().getDoubleExtra(INTENT_LOCATION_LAT, 0);
        lng = getIntent().getDoubleExtra(INTENT_LOCATION_LNG, 0);
        initMap();
    }

    /**
     * 设置地图UI相关参数
     */
    private void initMap() {
        LogUtils.d("lat:"+lat);
        LogUtils.d("lng:"+lng);
        uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false); // 隐藏缩放按钮

        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 14));
        addMark(lat, lng);
    }

    /**
     * 添加地图mark
     */
    private void addMark(double lat, double lng) {
        if (marker == null) {
            marker = aMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng))
                    .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_icon_chat_location_marker)))
                    .draggable(true));
        } else {
            marker.setPosition(new LatLng(lat, lng));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView.onSaveInstanceState(outState);
    }
}
